<template>
  <ele-modal
    form
    :width="738"
    :model-value="modelValue"
    :title="'打印-学生:'+studentObject.name+'('+studentObject.code+')'+'推荐表'"
    @update:modelValue="updateModelValue">
    <el-divider style="margin-top: 10px;margin-bottom: 10px;"></el-divider>
    <ele-loading :loading="loading" text="正在加载打印机" type="dot">
      <el-form :inline="true" :model="printerList" class="demo-form-inline">
        <el-form-item label="打印机">
          <el-select v-model="defaultPrinter" placeholder="请选择打印机" style="width: 350px;">
            <el-option v-for="(item,index) in printerList" :label="item" :value="item" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="goPrint">打印</el-button>
        </el-form-item>
      </el-form>
    </ele-loading>
    <el-divider style="margin-top: 0px;margin-bottom: 10px;"></el-divider>
    <div class="print-container">
      <div ref="frontPage" class="frontPage">
        <div class="container-05">
          <h1>福建省普通高校毕业生<br/>就&nbsp;&nbsp;业&nbsp;&nbsp;推&nbsp;&nbsp;荐&nbsp;&nbsp;表
          </h1>
          <div class="line-header">
            <div class="line">
              <div class="title">姓名：</div>
              <div class="info">{{ studentObject.name }}</div>
            </div>
            <div class="line">
              <div class="title">学校：</div>
              <div class="info">福州大学至诚学院</div>
            </div>
            <div class="line">
              <div class="title">专业：</div>
              <div class="info">{{ studentObject.majorDirection }}</div>
            </div>
            <div class="line">
              <div class="title">学历：</div>
              <div class="info">本科</div>
            </div>
            <div class="line">
              <div class="title">毕业时间：</div>
              <div class="info">{{ studentObject.graduationYear }}-7-1</div>
            </div>
          </div>
        </div>
      </div>
      <div ref="backPage" class="backPage">
        <div class="container-03">
          <table>
            <tr>
              <td style="width: 60px; min-width: 60px;">姓名</td>
              <td style="width: 90px;">{{ studentObject.name }}</td>
              <td style="width: 60px; min-width: 60px;">性别</td>
              <td style="width: 70px;">{{ studentObject.sex }}</td>
              <td style="width: 60px; min-width: 60px;">政治<br/>面貌</td>
              <td style="width: 100px;">{{ studentObject.politicalStatus }}</td>
              <td style="width: 60px; min-width: 60px;">毕业<br/>时间</td>
              <td style="width: 80px;">{{ studentObject.graduationYear }}-7-1</td>
              <td rowspan="3">{{ studentObject.code }}</td>
            </tr>
            <tr>
              <td>专业</td>
              <td colspan="3">{{ studentObject.majorDirection }}</td>
              <td>学制</td>
              <td>{{ studentObject.studyTime }}年</td>
              <td>学历</td>
              <td>本科</td>
            </tr>
            <tr>
              <td>外语<br/>水平</td>
              <td colspan="3">{{ studentObject.foreignLanguageEvel }}</td>
              <td>计算机<br/>水平</td>
              <td colspan="3">{{ studentObject.computerLevel }}</td>
            </tr>
            <tr>
              <td>个人<br/>电话</td>
              <td colspan="2">{{ studentObject.phoneNumber }}</td>
              <td>QQ</td>
              <td colspan="2">{{ studentObject.qq }}</td>
              <td>电子<br/>邮箱</td>
              <td colspan="2">{{ studentObject.email }}</td>
            </tr>
            <tr>
              <td>职业资<br/>格证书</td>
              <td colspan="12" class="text-left" style="height: 65px; padding-left: 35px; padding-right: 5px;">
                {{ studentObject.qualification }}
              </td>
            </tr>
            <tr>
              <td>家庭<br/>地址</td>
              <td colspan="12" class="text-left" style="height: 65px;padding-left: 35px; padding-right: 5px;">
                {{ studentObject.homeAddress }}
              </td>
            </tr>
            <tr class="line-01">
              <td style="height: 125px;">获奖<br/>荣誉</td>
              <td class="cell-body body-01" colspan="12">
                <div v-if="studentObject.rewardPunishmentPrize1 && studentObject.rewardPunishmentPrize1 != '无'">
                  {{ studentObject.rewardPunishmentPrize1 }}
                </div>
                <div v-if="studentObject.rewardPunishmentPrize2 && studentObject.rewardPunishmentPrize2 != '无'">
                  {{ studentObject.rewardPunishmentPrize2 }}
                </div>
                <div v-if="studentObject.rewardPunishmentPrize3 && studentObject.rewardPunishmentPrize3 != '无'">
                  {{ studentObject.rewardPunishmentPrize3 }}
                </div>
                <div v-if="studentObject.rewardPunishmentPrize4 && studentObject.rewardPunishmentPrize4 != '无'">
                  {{ studentObject.rewardPunishmentPrize4 }}
                </div>
                <div v-if="studentObject.rewardPunishmentPrize5 && studentObject.rewardPunishmentPrize5 != '无'">
                  {{ studentObject.rewardPunishmentPrize5 }}
                </div>
                <div
                  v-if="isEmptyString(studentObject.rewardPunishmentPrize1) && isEmptyString(studentObject.rewardPunishmentPrize2) && isEmptyString(studentObject.rewardPunishmentPrize3) && isEmptyString(studentObject.rewardPunishmentPrize4) && isEmptyString(studentObject.rewardPunishmentPrize5)">
                  无
                </div>
              </td>
            </tr>
            <tr class="line-01">
              <td style="height: 125px;">实践<br/>经历</td>
              <td class="cell-body body-01" colspan="12">
                <div v-if="studentObject.workContent1 && studentObject.workContent1 != '无'">
                  {{ studentObject.workContent1 }}
                </div>
                <div v-if="studentObject.workContent2 && studentObject.workContent2 != '无'">
                  {{ studentObject.workContent2 }}
                </div>
                <div v-if="studentObject.workContent3 && studentObject.workContent3 != '无'">
                  {{ studentObject.workContent3 }}
                </div>
                <div v-if="studentObject.workContent4 && studentObject.workContent4 != '无'">
                  {{ studentObject.workContent4 }}
                </div>
                <div v-if="studentObject.workContent5 && studentObject.workContent5 != '无'">
                  {{ studentObject.workContent5 }}
                </div>
                <div
                  v-if="isEmptyString(studentObject.workContent1) && isEmptyString(studentObject.workContent2) && isEmptyString(studentObject.workContent3) && isEmptyString(studentObject.workContent4) && isEmptyString(studentObject.workContent5)">
                  无
                </div>
              </td>
            </tr>
            <tr class="line-02">
              <td>求职<br/>意向</td>
              <td class="cell-body body-01" colspan="12">{{ studentObject.jobIntention }}</td>
            </tr>
            <tr class="line-03">
              <td>系推荐<br/>意见</td>
              <td class="cell-body body-01" colspan="12">
                {{ studentObject.departmentOpinion }}
              </td>
            </tr>
            <tr class="line-04">
              <td style="height: 120px;">学院<br/>推荐<br/>意见</td>
              <td class="fs-24" colspan="12">同意推荐就业！</td>
            </tr>
            <tr class="line-05">
              <td>学院<br/>联系人</td>
              <td class="cell-link" colspan="12">
                <span>{{ studentObject.counsellorName }}&nbsp;老师&nbsp;&nbsp;{{
                    studentObject.counsellorPhone
                  }}</span>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span>学院毕业生就业指导中心电话&nbsp;&nbsp;0591-87893159</span>
              </td>
            </tr>
          </table>
        </div>
      </div>
    </div>
  </ele-modal>
</template>

<script setup>
import {getLodop} from "@/assets/js/LodopFuncs";
import {ref, reactive, watch, onMounted} from 'vue';
import {EleMessage} from 'ele-admin-plus/es';
import {selectStudentRecommendInfo} from "@/api/manage/recommend/admin/index.js";


const emit = defineEmits(['done', 'update:modelValue']);
const props = defineProps({
  /** 弹窗是否打开 */
  modelValue: Boolean,
  /** 修改回显的数据 */
  data: String
});

/** 更新modelValue */
const updateModelValue = (value) => {
  emit('update:modelValue', value);
};

/** 提交状态 */
const loading = ref(false);
const studentObject = ref({
  name: '',
  code: ''
});

const frontPage = ref(null);
const backPage = ref(null);

const printStyle = ref(`
    .print-container * {
      padding: 0;
      margin: 0;
    }

    .print-container .pd-10 {
      padding: 10px;
    }

    .print-container .fs-24 {
      font-size: 24px;
    }

    .print-container .container-03 {
      width: 700px;
      height: 1000px;
      margin-left:auto;
      margin-right:auto;
      padding-top:40px;
      align-items: center;
    }

    .print-container .container-03 table {
      width: 700px;
      border-collapse: collapse;
    }

    .print-container .container-03 table td {
      height: 50px;
      border: solid 1px black;
      text-align: center;
    }

    .print-container .container-03 table .line-fr td {
      height: 1px;
      border: none;
    }

    .print-container .container-03 table .line-fr .cell-01 {
      width: 60px;
    }

    .print-container .container-03 table .line-fr .cell-02 {
      width: 46px;
    }

    .print-container .container-03 table .line-fr .cell-03 {
      width: 31px;
    }

    .print-container .container-03 table .line-fr .cell-04 {
      width: 49px;
    }

    .print-container .container-03 table .line-fr .cell-05 {
      width: 49px;
    }

    .print-container .container-03 table .line-fr .cell-06 {
      width: 49px;
    }

    .print-container .container-03 table .line-fr .cell-07 {
      width: 30px;
    }

    .print-container .container-03 table .line-fr .cell-08 {
      width: 30px;
    }

    .print-container .container-03 table .line-fr .cell-09 {
      width: 40px;
    }

    .print-container .container-03 table .line-fr .cell-10 {
      width: 49px;
    }

    .print-container .container-03 table .line-fr .cell-11 {
      width: 40px;
    }

    .print-container .container-03 table .line-fr .cell-12 {
      width: 40px;
    }

    .print-container .container-03 table .line-fr .cell-13 {
      width: 100px;
    }

    .print-container .container-03 table .line-01 td {
      height: 107px;
    }

    .print-container .container-03 table .line-02 td {
      height: 60px;
    }

    .print-container .container-03 table .line-03 td {
      height: 145px;
    }

    .print-container .container-03 table .line-04 td {
      height: 60px;
    }

    .print-container .container-03 table .line-05 td {
      height: 60px;
    }

    .print-container .container-03 table .cell-title {
      padding: 0 30px;
    }

    .print-container .container-03 table .cell-body {
      text-align: left;
      vertical-align: top;
      text-indent: 2em;
      padding: 0 5px 0 0;
    }

    .print-container .container-03 table .cell-body.body-01 {
      vertical-align: middle;
      padding-left: 5px;
      padding-right: 5px;
    }

    .print-container .container-03 table .cell-link p:first-child {
      float: left;
      padding: 0 0 0 10px;
    }

    .print-container .container-03 table .text-left {
      text-align: left;
      padding: 0 30px;
    }

    .print-container .container-04 {
      width: 102px;
      height: 100%;
      padding: 890px 0;
      text-align: center;
    }

    .print-container .container-05 {
      width: 700px;
      height: 808px;
      margin-left:auto;
      margin-right:auto;
      text-align: center;
      padding-top: 230px;
    }

    .print-container .container-05 .line-header {
      margin-left: -50px;
    }
    .print-container .container-05 h1 {
      font-size: 49px;
      margin: 0px 0 355px 0;
      font-family:微软雅黑;
    }

    .print-container .container-05 .line {
      display: flex;
      justify-content: center;
      font-size: 20px;
      height: 45px;
      align-items: center;
    }

    .print-container .container-05 .line .title {
      font-weight: bold;
      width: 120px;
      text-align: right;
      font-family:微软雅黑;
    }

    .print-container .container-05 .line .info {
      border-bottom: solid black 1px;
      width: 290px;
    }


  `)


const goPrint = () => {
  let frontPageHtml = frontPage.value.innerHTML;
  let backPageHtml = backPage.value.innerHTML;

  let LODOP = getLodop()//调用getLodop获取LODOP对象
  let htmlEnd = "</html>";

  //添加样式
  let strStyleCSS = "<!DOCTYPE html><html><head><style type=\"text/css\">" + printStyle.value + "</style></head>";
  LODOP.PRINT_INIT("打印毕业生推荐表");//初始化打印任务

  let strHtml = "<div class='print-container'>" + frontPageHtml + "</div>";
  LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4");//设置纸张样式
  // LODOP.SET_PRINT_STYLEA(0, "Horient", 2);//水平垂直居中
  // LODOP.SET_PRINT_STYLEA(0, "Vorient", 2);//水平垂直居中
  LODOP.ADD_PRINT_HTM(0, 0, "100%", "100%", strStyleCSS + strHtml + htmlEnd);//设置打印内容
  LODOP.SET_PRINT_MODE("PRINT_PAGE_PERCENT", "100%");//设置缩放比例
  // console.log(strStyleCSS + strHtml + htmlEnd);
  // LODOP.PRINT_DESIGN();
  LODOP.NewPage();
  strHtml = "<div class='print-container'>" + backPageHtml + "</div>";

  LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4");//设置纸张样式
  // LODOP.SET_PRINT_STYLEA(0, "Horient", 2);//水平垂直居中
  // LODOP.SET_PRINT_STYLEA(0, "Vorient", 2);//水平垂直居中
  LODOP.ADD_PRINT_HTM(0, 0, "100%", "100%", strStyleCSS + strHtml + htmlEnd);//设置打印内容

  LODOP.SET_SHOW_MODE("MESSAGE_PARSING_HTM", "正在加载要打印的内容，请稍等！");//设置消息显示模式
  LODOP.SET_PRINT_MODE("PRINT_DEFAULTSOURCE", 7);//设置纸盒 7-自动
  //LODOP.SET_PRINT_MODE("FULL_WIDTH_FOR_OVERFLOW",true);
  LODOP.SET_PRINT_MODE("PRINT_PAGE_PERCENT", "100%");//设置缩放比例
  LODOP.SET_PRINTER_INDEXA(defaultPrinter.value);//设置打印机
  LODOP.SET_PRINT_MODE("PRINT_DUPLEX", 2);//长边双面打印
  LODOP.PREVIEW();//打印预览
  // LODOP.PRINT_DESIGN();
  return;
}


const selectCheckInfo = () => {
  // loading.value = true;
  selectStudentRecommendInfo(props.data).then(response => {
    studentObject.value.name = response.data.name;
    studentObject.value.code = response.data.code;

    studentObject.value.phoneNumber = response.data.phoneNumber;
    studentObject.value.politicalStatus = response.data.politicalStatus;
    studentObject.value.qq = response.data.qq;
    studentObject.value.qualification = response.data.qualification;

    studentObject.value.computerLevel = response.data.computerLevel;
    studentObject.value.email = response.data.email;
    studentObject.value.foreignLanguageEvel = response.data.foreignLanguageEvel;
    studentObject.value.homeAddress = response.data.homeAddress;
    studentObject.value.homeCode = response.data.homeCode;
    studentObject.value.homePhone = response.data.homePhone;
    studentObject.value.isCityStudent = response.data.isCityStudent;
    studentObject.value.isHkInSchool = response.data.isHkInSchool;
    studentObject.value.jobIntention = response.data.jobIntention;

    studentObject.value.rewardPunishmentPrize1 = response.data.rewardPunishmentPrize1;
    studentObject.value.rewardPunishmentPrize2 = response.data.rewardPunishmentPrize2;
    studentObject.value.rewardPunishmentPrize3 = response.data.rewardPunishmentPrize3;
    studentObject.value.rewardPunishmentPrize4 = response.data.rewardPunishmentPrize4;
    studentObject.value.rewardPunishmentPrize5 = response.data.rewardPunishmentPrize5;

    studentObject.value.rxqdaszdw = response.data.rxqdaszdw;
    studentObject.value.rxqhkszdpcs = response.data.rxqhkszdpcs;

    studentObject.value.workContent1 = response.data.workContent1;
    studentObject.value.workContent2 = response.data.workContent2;
    studentObject.value.workContent3 = response.data.workContent3;
    studentObject.value.workContent4 = response.data.workContent4;
    studentObject.value.workContent5 = response.data.workContent5;

    studentObject.value.departmentOpinion = response.data.departmentOpinion;
    studentObject.value.studentRecommendSubmit = response.data.studentRecommendSubmit;


    studentObject.value.campus = response.data.campus;
    studentObject.value.grade = response.data.grade;
    studentObject.value.department = response.data.department;
    studentObject.value.major = response.data.major;
    studentObject.value.majorDirection = response.data.majorDirection;
    studentObject.value.studyTime = response.data.studyTime;
    studentObject.value.classNum = response.data.classNum;
    studentObject.value.graduationYear = response.data.graduationYear;
    studentObject.value.enterTime = response.data.enterTime;
    studentObject.value.graduateTime = response.data.graduateTime;

    studentObject.value.counsellorName = response.data.counsellorName;
    studentObject.value.counsellorPhone = response.data.counsellorPhone;
    studentObject.value.sex = response.data.sex;
  })
}

// const cssBlock = document.styleSheets
// console.log('cssBlock:', cssBlock)

const printerList = ref([]);
const defaultPrinter = ref('');

/**
 * 初始化本地打印机列表
 */
const initPrinterList = () => {
  let LODOP = getLodop()//调用getLodop获取LODOP对象
  //初始化打印机列表
  let printCount = LODOP.GET_PRINTER_COUNT();//获取打印机数量
  for (let i = 0; i < printCount; i++) {
    printerList.value.push(LODOP.GET_PRINTER_NAME(i));
  }
  defaultPrinter.value = LODOP.GET_PRINTER_NAME(-1);//获取默认打印机
  // console.log('默认打印机是:' + typeof (defaultPrinter.value));
  // console.log('默认打印机是:' + LODOP.GET_PRINTER_NAME(-1));
}

/**
 * 判断字符串是否为空
 * @param str
 * @returns {boolean}
 */
const isEmptyString = (str) => {
  if (str === null || str === undefined || str.trim() === '') {
    return true; // 字符串为空
  } else {
    return false; // 字符串不为空
  }
}


onMounted(() => {
  loading.value = true;
  //加载学生信息
  selectCheckInfo();
  //延迟1.5秒再执行
  setTimeout(function () {
    loading.value = false;
    initPrinterList();
  }, 1500); // 1500毫秒后执行

  //注册样式
  const style = document.createElement('style');
  style.innerHTML = printStyle.value;
  document.head.appendChild(style);
})


</script>
